/*
 * Created on Mar 9, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package littlecyberwar.control;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import javax.swing.tree.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.event.*;
import java.io.*;
import java.beans.XMLEncoder;

import ModuleBuilder.control.SimpleFileFilter;
import ModuleBuilder.model.*;
import ModuleBuilder.UI.tree.*;
import littlecyberwar.util.*;

/**
 * @author Milton  Soong
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class Storer {
	private static JFileChooser fc = new JFileChooser();
	
	public static ScenarioModel storeScenarioModel( JFrame parent ) {
		ScenarioModel scenarioMod = ScenarioManager.getScenarioModel();
		
		//Show it.
		fc.resetChoosableFileFilters();
		fc.addChoosableFileFilter(new SimpleFileFilter(Loader.scenarios, "Scenario Builder Scenario files"));
		int returnVal = fc.showDialog(parent, "Select Game Save File Name");
		File file=null;
		
		//Process the results.
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fc.getSelectedFile();
			String fn = file.getPath();

			// make sure the file ends in the right suffix
			String lowerCaseFn = fn.toLowerCase();
			if (!lowerCaseFn.endsWith(Loader.scenarios[0])){
				fn += Loader.scenarios[0];
				file = new File(fn);
			}


		} else {
			Debug.out("Select Scenario File cancelled by user.");
			return ( null );
		}
		// now serializr the result
		FileOutputStream fo=null;
		BufferedOutputStream bo=null;
		ObjectOutputStream oo = null;
		try {
			fo = new FileOutputStream(file);
			bo = new BufferedOutputStream(fo);
			oo = new ObjectOutputStream(bo);
			oo.writeObject(scenarioMod);
			
			oo.flush();
			oo.close();
			
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return ( scenarioMod );
	}

	public static ScenarioModel storeScenarioModelXML( JFrame parent ) {
		ScenarioModel scenarioMod = ScenarioManager.getScenarioModel();
		
		//Show it.
		fc.resetChoosableFileFilters();
		fc.addChoosableFileFilter(new SimpleFileFilter(Loader.scenarios, "Scenario Builder Scenario files"));
		int returnVal = fc.showDialog(parent, "Select Game Save File Name");
		File file=null;
		
		//Process the results.
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fc.getSelectedFile();
			String fn = file.getPath();

			// make sure the file ends in the right suffix
			String lowerCaseFn = fn.toLowerCase();
			if (!lowerCaseFn.endsWith(Loader.scenarios[0])){
				fn += Loader.scenarios[0];
				file = new File(fn);
			}


		} else {
			Debug.out("Select Scenario File cancelled by user.");
			return ( null );
		}
		// now serializr the result
		FileOutputStream fo=null;
		BufferedOutputStream bo=null;
		// ObjectOutputStream oo = null;
		try {
			fo = new FileOutputStream(file);
			bo = new BufferedOutputStream(fo);
			// oo = new ObjectOutputStream(bo);
			XMLEncoder e = new XMLEncoder(bo);
			
			e.writeObject(scenarioMod);
			
			e.flush();
			e.close();
			
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return ( scenarioMod );
	}

	/**
	 * @return
	 */
	public static JFileChooser getFc() {
		return fc;
	}

}
